package com.vodone.v1pin.core
{
	import com.vodone.v1pin.core.event.V1webEventDispatcher;
	import com.vodone.v1pin.core.model.ConditionVO;
	import com.vodone.v1pin.core.model.MainModelLocator;
	import com.vodone.v1pin.core.utils.GoodsUtil;
	import com.vodone.v1pin.core.utils.LoginUtil;
	import com.vodone.v1pin.index.vo.PicVO;
	import com.vodone.v1pin.navigator.event.NavigatorChangeEvent;
	import com.vodone.v1pin.navigator.view.Navigator;
	import com.vodone.v1pin.userInfo.event.CloseWindowEvent;
	
	import flash.external.ExternalInterface;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	
	import mx.managers.PopUpManager;

	public class ExternalInterfaceManager
	{
		/**
		 *  
		 */	
		public static var isWeb:Boolean = false;
		/**
		 * 首页 
		 */		
		public static const INDEX:String = "index";
		/**
		 * 用户页 
		 */		
		public static const USER:String = "user";
		
		/**
		 * flash加载完成 
		 * 
		 */		
		public static function checkInWeb():void
		{
			var b:String = ExternalInterface.call( "checkInWeb" );
			
			
			if( b ){
				isWeb = true;
			}else{
				isWeb = false;
			}
			MainModelLocator.getInstance().debugStr = "是否是发布状态：" + isWeb + "\n";
		}
		/**
		 * flash加载完成 
		 * 
		 */		
		public static function productComplete():void
		{
			ExternalInterface.call( "productComplete" );
		}
		
		/**
		 * 通过url跳转 
		 * 
		 */		
		public static function JumpURL( url:String = null ):void
		{
			if( url == null ) return;
			
			if( url.indexOf("v1pin") == -1 && url.indexOf("v1test") == -1){
				var request:URLRequest=new URLRequest(url);
				navigateToURL(request,"_blank");  
			}else{
				new CloseWindowEvent(CloseWindowEvent.JUMPURL_EVENT,url).dispatch();
			}
			
		}
		/**
		 * 通过导航跳转 
		 * @param navLevel
		 * @param navId
		 * 
		 */		
		public static function NavtatorURL( subApp:String , currentId:String = null , condition:ConditionVO = null , obj:Object = null , sendEvt:Boolean = true ):void
		{
			
			var id:String;
			if( currentId != null ){
				var idList:Array = currentId.split("#");
				if( idList.length > 0 ){
					id = idList[idList.length-1];
				}
			}
			
			ExternalInterface.call( "NavtatorURL" , subApp , id, condition , obj );
			var event:NavigatorChangeEvent = new NavigatorChangeEvent( subApp , id, condition , obj );
			if(sendEvt){
				event.dispatch();
			}
			
			LoginUtil.checkLogin();
			MainModelLocator.getInstance().currentURL = ExternalInterface.call("window.location.href.toString");
		}
		
		/**
		 * 非通过导行跳转 
		 * @param module
		 * @param channel
		 * 
		 */		
		public static function changeModuleView( module:String , channel:int = 0 ):void
		{
			ExternalInterface.call( "changeModuleView" , module , channel );
			MainModelLocator.getInstance().currentURL = ExternalInterface.call("window.location.href.toString");
		}
		
		/**
		 * 数据流村
		 * 
		 */		
		public static function logID( sendURL:String ):void
		{
			ExternalInterface.call("ggan",sendURL);
			trace(sendURL);
		}
		/**
		 * 图片跳转 
		 * @param module
		 * @param channel
		 * 
		 */		
		public static function clickPicJump( pic:PicVO ):void
		{
			if( pic == null ) return;
			if( ConditionVO.parasModule(pic.subApp) == ConditionVO.DETAIL )
			{
				MyPopUpManager.getInstance().openDetailView( pic.id );
				return;
			} else if( ConditionVO.parasModule(pic.subApp) == ConditionVO.PRODUCTLIST ){
				ExternalInterfaceManager.NavtatorURL( pic.subApp , pic.id );
				return;
			}
			if( pic.navUrl != "" ){
				JumpURL(pic.navUrl);
			}
		}
		
		/**
		 * 弹出window框 
		 * @param module
		 * @param channel
		 */		
		public static function openWindow( module:String , id:String = "", channel:String = "" ):void
		{
			if( module != "" )
			{
				MainModelLocator.getInstance().isWindowOpen = true;
			}else{
				new CloseWindowEvent(CloseWindowEvent.CLOSE_WINDOW_EVENT).dispatch();
				MainModelLocator.getInstance().isWindowOpen = false;
			}
			LoginUtil.checkLogin();
			PopUpManager.bringToFront(MainModelLocator.getInstance().kfPop);
			ExternalInterface.call( "openWindow" , module , id );
			MainModelLocator.getInstance().currentURL = ExternalInterface.call("window.location.href.toString");
		}
	}
}